# _*_ coding: utf-8 _*_

import csv
import sys
import numpy as np
import matplotlib.pyplot as plt
from sqlalchemy import func 
from CreateBaseDados import *

#Numero de vagas sobrantes por  instituicao e distrito?


distritos = ["Aveiro", "Braga", "Braganca", "VilaReal", "Viana do Castelo", "Porto", 
			 "Guarda", "Castelo Branco", "Viseu", "Coimbra", " Portalegre",
			"Evora", " Setubal", "Lisboa", "Leiria", "Beja", "Faro", "Santarem", "Madeira", "Acores" ]


			
alunos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

filename = "output1.csv"

spamWriter = csv.writer(open(filename, 'w'), delimiter=',', quotechar='\x22', quoting=csv.QUOTE_MINIMAL)

x = 0

for distrito in distritos:
	for obj in session.query(Dados).filter(Dados.nomeInstituicao.like("%" + distrito + "%")):
		alunos[x] = alunos[x] + int(obj.vagasSobrantes)
	spamWriter.writerow([distrito.encode('utf-8')] + [alunos[x]])
	x = x + 1

# imprime no grafico

y_pos = np.arange(len(distritos))

performance = np.arange(len(distritos))

error = np.arange(len(distritos))

plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)

plt.yticks(y_pos, distritos )

plt.xlabel('alunos')

plt.grid(True)

plt.title('Numero de vagas por colocar por Distrito')

plt.grid(True)

plt.show()

